<?php

/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */
 
namespace app\admin\logic;

use think\Model;
use think\Db;

class UsersLogic extends Model
{    
    
    /**
     * 获取指定用户信息
     * @param $uid int 用户UID
     * @param bool $relation 是否关联查询
     *
     * @return mixed 找到返回数组
     */
    public function detail($uid, $relation = true)
    {
        $user = M('users')->where(array('user_id' => $uid))->relation($relation)->find();
        return $user;
    }
    
    /**
     * 改变用户信息
     * @param int $uid
     * @param array $data
     * @return array
     */
    public function updateUser($uid = 0, $data = array())
    {
        $db_res = M('users')->where(array("user_id" => $uid))->data($data)->save();
        if ($db_res) {
            return array(1, "用户信息修改成功");
        } else {
            return array(0, "用户信息修改失败");
        }
    }
    
    
    /**
     * 添加用户
     * @param $user
     * @return array
     */
    public function addUser($user)
    {
		$user_count = Db::name('users')
				->where(function($query) use ($user){
					if ($user['email']) {
						$query->where('email',$user['email']);
					}
					if ($user['mobile']) {
						$query->whereOr('mobile',$user['mobile']);
					}
				})
				->count();
		if ($user_count > 0) {
			return array('status' => -1, 'msg' => '账号已存在');
		}
    	$user['password'] = encrypt($user['password']);
    	$user['reg_time'] = time();
    	$user_id = M('users')->add($user);
    	if(!$user_id){
    		return array('status'=>-1,'msg'=>'添加失败');
    	}else{
    		$pay_points = tpCache('basic.reg_integral'); // 会员注册赠送积分
    		if($pay_points > 0)
    			accountLog($user_id, 0 , $pay_points , '会员注册赠送积分'); // 记录日志流水
    		return array('status'=>1,'msg'=>'添加成功','user_id'=>$user_id);
    	}
    }   
    public function checkPuser($parentid,$level,$name)
    {
       $res = M('users')->find($parentid);

       if($res){
           $cxhurmoney = 700;
           $cxhurmoney2 = 300;
           $mzmoney = 1200;
           $mzmoney2 = 300;
           //如果是发展城市合伙人
           if($level==2){
               Db::name('users')->where('user_id',$parentid)->setInc("friend_money",$cxhurmoney);
               Db::name('users')->where('user_id',$parentid)->setInc("total_amount",$cxhurmoney);
               Db::name('users')->where('user_id',$parentid)->setInc("friend_num",1);
               if($res['friend_num']==4){
                   Db::name('users')->where('user_id',$parentid)->setDec("distribut_level",1);
               }
               $log['user_id'] = $parentid;
               $log['money'] = $cxhurmoney;
               $log['record'] = '推荐城市合伙人'.$name.'+'.$cxhurmoney;
               $log['type'] = 1;
               Db::name('users_rebate_log')->add($log);
               accountLog($parentid,$cxhurmoney,0,'推荐城市合伙人'.$name.'+'.$cxhurmoney,0);
               if($res['parent_id']){
                   $parentres = Db::name('users')->where('user_id',$res['parent_id'])->find();
                   if($parentres['distribut_level']==1){
                       Db::name('users')->where('user_id',$res['parent_id'])->setInc("next_friend_money",$cxhurmoney2);
                       Db::name('users')->where('user_id',$res['parent_id'])->setInc("total_amount",$cxhurmoney2);
                       Db::name('users')->where('user_id',$res['parent_id'])->setInc("next_friend_num",1);

                       $log['user_id'] = $res['parent_id'];
                       $log['money'] = $cxhurmoney2;
                       $log['record'] = '下级推荐城市合伙人'.$name.'+'.$cxhurmoney2;
                       $log['type'] = 3 ;
                       Db::name('users_rebate_log')->add($log);
                       accountLog($res['parent_id'],$cxhurmoney2,0,'下级推荐城市合伙人'.$name.'+'.$cxhurmoney2,0);
                   }

               }

           }else if($level==3){
               //推荐门诊
               Db::name('users')->where('user_id',$parentid)->setInc("next_mzmoney",$mzmoney);
               Db::name('users')->where('user_id',$parentid)->setInc("total_amount",$mzmoney);
               Db::name('users')->where('user_id',$parentid)->setInc("next_mznum",1);

               $log['user_id'] = $parentid;
               $log['money'] = $mzmoney;
               $log['record'] = '推荐门诊+'.$mzmoney;
               $log['type'] = 2;
               Db::name('users_rebate_log')->add($log);
               accountLog($parentid,$mzmoney,0,'推荐门诊+'.$mzmoney,0);
               if($res['parent_id']){
                   $parentres = Db::name('users')->where('user_id',$res['parent_id'])->find();
                   if($parentres['distribut_level']==1) {
                       Db::name('users')->where('user_id', $res['parent_id'])->setInc("second_mzmoney", $mzmoney2);
                       Db::name('users')->where('user_id', $res['parent_id'])->setInc("total_amount", $mzmoney2);
                       Db::name('users')->where('user_id', $res['parent_id'])->setInc("second_mznum", 1);

                       $log['user_id'] = $res['parent_id'];
                       $log['money'] = $mzmoney2;
                       $log['record'] = '间接推荐门诊' . $name . '+' . $mzmoney2;
                       $log['type'] = 3;
                       Db::name('users_rebate_log')->add($log);
                       accountLog($res['parent_id'], $mzmoney2, 0, '间接推荐门诊' . $name . '+' . $mzmoney2, 0);
                   }
               }
           }

       }
    }
    public function accountAdd($uid){
        $res = Db::name('users')->find($uid);
        $accountInfo['user_name'] = $res['mobile'];
        $accountInfo['password'] = $res['password'];
        $accountInfo['add_time'] = time();
        $accountInfo['role_id'] = 18;
        $accountInfo['user_id'] = $uid;
        Db::name('admin')->add($accountInfo);
    }

    public function accountedit($uid){
        $res = Db::name('users')->find($uid);
        $accountInfo['user_name'] = $res['mobile'];
        $accountInfo['password'] = $res['password'];
        $accountInfo['nickname'] = $res['nickname'];
        $accountInfo['add_time'] = time();
        $accountInfo['role_id'] = 18;
        Db::name('admin')->where('user_id',$uid)->save($accountInfo);
    }
}